//
//  SendSubjectImagesView.m
//  xiaoyiNew
//
//  Created by TW on 2017/3/2.
//  Copyright © 2017年 讯一. All rights reserved.
//

#import "SendSubjectImagesView.h"
#import "SendSubjectImagesCollectionViewCell.h"

@interface SendSubjectImagesView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIGestureRecognizerDelegate>
@property(nonatomic, strong) UICollectionView * collectionView;//
@property(nonatomic, strong) NSMutableArray * imagesArr;//图片数组
@property(nonatomic, assign) BOOL ifChange;//
@property(nonatomic, assign) NSInteger imagesNum;//
@property (nonatomic,strong) UILongPressGestureRecognizer * longPressGr;//长按手势
@end

@implementation SendSubjectImagesView

- (void)awakeFromNib {

    [super awakeFromNib];
    self.imagesNum = 0;
    self.ifChange = YES;
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

- (void)setImagesNum:(NSInteger)imagesNum {

    if (imagesNum >= 9) {
        self.ifChange = NO;
        _imagesNum = 9;
    }else {
    
        _imagesNum = imagesNum;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(changeHeightWithIndex:)]) {
        [_delegate changeHeightWithIndex:imagesNum];
    }
}
//长按方法
- (void)longPressToDo:(UILongPressGestureRecognizer *)gestureRecognizer {
    
    if(gestureRecognizer.state !=UIGestureRecognizerStateEnded) {
        
        CGPoint p = [gestureRecognizer locationInView:self.collectionView];
        
        NSIndexPath * indexPath = [self.collectionView indexPathForItemAtPoint:p];
        if(indexPath ==nil){
            
            NSLog(@"没有找到");
        }else if (indexPath.row == 0){
            NSLog(@"0");
        }else {
        
            [self.imagesArr removeObjectAtIndex:indexPath.row - 1];
            self.imagesNum --;
            [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
        }
    }
}
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource -

//返回分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//返回每个分区的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1 + self.imagesArr.count;
}

//返回每个item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SendSubjectImagesCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    if (indexPath.row == 0) {
        cell.logoImage.image = [UIImage imageNamed:@"xiangji.png"];
        cell.layer.borderColor = UIColorFromHex(0x777777).CGColor;
        cell.layer.borderWidth = 1;
        cell.layer.cornerRadius = 3;
        cell.layer.masksToBounds = YES;
    }else {
    
        cell.logoImage.image = [UIImage imageWithData:_imagesArr[indexPath.row-1]];
    }
    
    return cell;
}
//点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
    if (indexPath.row == 0 && self.ifChange) {
        UIImagePickerController *pickImag = [[UIImagePickerController alloc]init];
        pickImag.delegate = self;
        pickImag.allowsEditing = YES;
        pickImag.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        if (_delegate && [_delegate respondsToSelector:@selector(pushToPhotoPage:)]) {
            [_delegate pushToPhotoPage:pickImag];
        }
//        [self showViewController:pickImag sender:nil];
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
#pragma mark --  UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *imag = info[UIImagePickerControllerEditedImage];
    
    UIImage *saveImage = imag;
    NSData *sizeData = UIImageJPEGRepresentation(saveImage, 1);//计算图片大小的nsdata
    CGFloat  size = [sizeData length]/1024;
    NSData *data = [NSData data];
    if (size > 200) {//如果图片大小大于200K
        data = UIImageJPEGRepresentation(saveImage, 60/size);
        CGFloat size1 = [data length]/1024;
        NSLog(@"压缩图片大小 %lf",size1);
    }else{//小与200K就本大小
        NSLog(@"不压缩图片");
        data = UIImageJPEGRepresentation(saveImage, 1);
        CGFloat size1 = [data length]/1024;
        NSLog(@"图片大小 %lf",size1);
    }
//    [self.headImageButton setImage:[UIImage imageWithData:data] forState:0];
    [self.imagesArr addObject:data];
    self.imagesNum ++;
    [self.collectionView reloadData];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark - lazy -
- (UILongPressGestureRecognizer *)longPressGr {
    
    if(!_longPressGr){
        _longPressGr =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
        _longPressGr.minimumPressDuration=1.0;
        _longPressGr.delegate=self;
        _longPressGr.delaysTouchesBegan=YES;
    }
    return _longPressGr;
}
- (NSMutableArray *)imagesArr {

    if (!_imagesArr) {
        _imagesArr = [NSMutableArray array];
    }
    return _imagesArr;
}
-(UICollectionView *)collectionView {
    
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((CGRectGetWidth(self.bounds) - 60)/4, (CGRectGetWidth(self.bounds) - 60)/4);
        layout.minimumInteritemSpacing = 5;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) collectionViewLayout:layout];
        [_collectionView registerClass:[SendSubjectImagesCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView addGestureRecognizer:self.longPressGr];
    }
    return _collectionView;
}

@end
